<template>
  <div class="app-container">
    <el-table v-loading="listLoading" :data="list" border fit highlight-current-row style="width: 100%">
      <el-table-column align="center" label="编号" width="80">
        <template slot-scope="{row}">
          <router-link :to="'/res/edit/'+row.id" class="link-type">
            <span>{{ row.id }}</span>
          </router-link>
        </template>
      </el-table-column>

      <el-table-column width="300px" align="center" label="设备组">
        <template slot-scope="scope">
          <span>{{ scope.row.MEQG_Name }}</span>
        </template>
      </el-table-column>

      <el-table-column width="200px" align="center" label="设备编号">
        <template slot-scope="scope">
          <span>{{ scope.row.Code }}</span>
        </template>
      </el-table-column>

      <el-table-column width="200px" align="center" label="设备名">
        <template slot-scope="scope">
          <span>{{ scope.row.Name }}</span>
        </template>
      </el-table-column>

      <el-table-column width="250px" align="center" label="排序">
        <template slot-scope="scope">
          <span>{{ scope.row.U_VisOrder }}</span>
        </template>
      </el-table-column>

      <el-table-column width="100px" align="center" label="状态">
        <template slot-scope="scope">
          <span>{{ scope.row.Canceled == 'Y' ? '停用' : '启用' }}</span>
        </template>
      </el-table-column>

      <el-table-column align="center" label="操作" min-width="220">
        <template slot-scope="scope">
          <router-link :to="'/res/edit/'+scope.row.id">
            <el-button type="primary" size="small" icon="el-icon-edit">
              Edit
            </el-button>
          </router-link>

          <el-button @click="handleResEnableOrDisable(scope.row)" :type="scope.row.Canceled == 'Y'?'success':'danger'" size="small" icon="el-icon-connection" style="margin-left:10px">
            {{ scope.row.Canceled == 'Y' ? '启用' : '停用'}}
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />
  </div>
</template>

<script>
import axios from 'axios'
import { fetchList, updateMEQUEnableOrDisable } from '@/api/res'
import Pagination from '@/components/Pagination' // Secondary package based on el-pagination

export default {
  name: 'ResList',
  components: { Pagination },
  data() {
    return {
      list: null,
      flist: null,
      total: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 20
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getTestList(){
      this.list = [
        {id:1,MEQG_Code:'HJ',MEQG_Name:'混胶',Code:'A-1',Name:'A-1',U_VisOrder:'A1',Canceled:'N'},
        {id:2,MEQG_Code:'HJ',MEQG_Name:'混胶',Code:'A-2',Name:'A-2',U_VisOrder:'A2',Canceled:'N'},
        {id:3,MEQG_Code:'HJ',MEQG_Name:'混胶',Code:'A-3',Name:'A-3',U_VisOrder:'A3',Canceled:'N'},
        {id:4,MEQG_Code:'HJ',MEQG_Name:'混胶',Code:'A-4',Name:'A-4',U_VisOrder:'A4',Canceled:'N'},
        {id:5,MEQG_Code:'HJ',MEQG_Name:'混胶',Code:'B-1',Name:'B-1',U_VisOrder:'A5',Canceled:'N'},
        {id:6,MEQG_Code:'HJ',MEQG_Name:'混胶',Code:'B-2',Name:'B-2',U_VisOrder:'A6',Canceled:'N'},
        {id:7,MEQG_Code:'HJ',MEQG_Name:'混胶',Code:'B-3',Name:'B-3',U_VisOrder:'A7',Canceled:'N'},
        {id:8,MEQG_Code:'HJ',MEQG_Name:'混胶',Code:'B-4',Name:'B-4',U_VisOrder:'A8',Canceled:'N'}
      ]
      this.total = this.list.length
      this.listLoading = false
    },
    handleResEnableOrDisable(row){
      var config ={
        Code: row.Code,
        Status: row.Canceled == 'Y' ? 'on' : 'off'
      }
      updateMEQUEnableOrDisable(config).then(res=>{
        row.Canceled = row.Canceled == 'Y' ? 'N' : 'Y'
        console.log('handleResEnableOrDisable---------',config,res)
      }).catch(err=>{
        console.log('handleResEnableOrDisable--err-------',config,err)
      })
    },

    getList() {
      this.listLoading = true

      fetchList(this.listQuery).then(res=>{
        const { flist, curPageList} = res

        this.flist = flist
        this.list = curPageList
        this.total = flist.length

        this.listLoading = false

      }).catch(err=>{
        console.log('error submit!!',err);
        this.listLoading = false
      })

    }
  }
}
</script>

<style scoped>
.edit-input {
  padding-right: 100px;
}
.cancel-btn {
  position: absolute;
  right: 15px;
  top: 10px;
}
</style>
